package com.wxw.gulidemo.controller.front;/**
 * @author by xxz
 * @date 2022/6/16
 */

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxw.gulidemo.R;
import com.wxw.gulidemo.client.OrderClient;
import com.wxw.gulidemo.entity.EduCourse;
import com.wxw.gulidemo.entity.EduTeacher;
import com.wxw.gulidemo.entity.vo.ChapterVo;
import com.wxw.gulidemo.entity.vo.frontVo.CourseFrontVo;
import com.wxw.gulidemo.entity.vo.frontVo.CourseWebVo;
import com.wxw.gulidemo.service.IEduChapterService;
import com.wxw.gulidemo.service.IEduCourseService;
import com.wxw.gulidemo.service.impl.EduCourseServiceImpl;
import com.wxw.gulidemo.utils.JwtUtils;
import net.bytebuddy.implementation.bind.annotation.Empty;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Description

 * @author by xxz

 * @date 2022/6/16

 * @throws
 */
@RestController
@CrossOrigin
@RequestMapping("/eduService/courseFront")
public class CourseFrontController {

    @Autowired
    private IEduCourseService eduCourseService;

    @Autowired
    private IEduChapterService eduChapterService;

    @Autowired
    private OrderClient orderClient;

    //课程列表(带条件、分页)
    @PostMapping("/getCourseFrontPageList/{page}/{limit}")
    public R getCourseFrontPageList(@PathVariable Long page,
                                    @PathVariable Long limit,
                                    @RequestBody(required = false) CourseFrontVo courseFrontVo){
        Page<EduCourse> coursePage = new Page<>(page, limit);
        Map<String,Object> map = eduCourseService.getCourseFrontPageList(coursePage,courseFrontVo);
        //返回分页中的所有数据
        return R.ok().data(map);
    }

    //课程详情信息展示(+根据支付状态来显示 -- 立即购买 or 立即观看)
    @GetMapping("/getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable("courseId") String courseId,
                                HttpServletRequest request){
        boolean isBuyCourse = false;

        //根据课程id，编写sql语句查询课程信息
        CourseWebVo courseWebVo = eduCourseService.getBaseCourseInfo(courseId);

        //根据课程id，查询章节和小节信息
        List<ChapterVo> chapterVideoList = eduChapterService.getChapterVideoByCourseId(courseId);

        //获取用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);

        if (!StringUtils.isEmpty(memberId)){
            //根据课程id、用户id，查询课程是否已经购买
            isBuyCourse = orderClient.isBuyCourse(memberId, courseId);
        }

        return R.ok().data("courseWebVo",courseWebVo).data("chapterVideoList",chapterVideoList).data("isBuy",isBuyCourse);

    }

}
